/*****************************************************************************/
/* Include Files */

#include "Common.h"

/*****************************************************************************/
/* Function Definitions */

TUINT16 Read_TUINT16(const TUINT8* buffer,
                     TUINT32       position)
{
    return ((TUINT16) buffer[position]) + (((TUINT16) buffer[position + 1]) << 8);
}

TUINT32 Read_TUINT32(const TUINT8* buffer,
                     TUINT32       position)
{
    return ((TUINT32) buffer[position]) + (((TUINT32) buffer[position + 1]) << 8) + (((TUINT32) buffer[position + 2]) << 16) + (((TUINT32) buffer[position + 3]) << 24);
}

void Write_TUINT16(TUINT8* buffer,
                   TUINT32 position,
                   TUINT16 data)
{
    ((TUINT8*) buffer)[position] = (TUINT8) (data&0x00FF);
    ((TUINT8*) buffer)[position + 1] = (TUINT8) ((data&0xFF00) >> 8);
}

void Write_TUINT32(TUINT8* buffer,
                   TUINT32 position,
                   TUINT32 data)
{
    ((TUINT8*) buffer)[position] = (TUINT8) (data&0x000000FF);
    ((TUINT8*) buffer)[position + 1] = (TUINT8) ((data&0x0000FF00) >> 8);
    ((TUINT8*) buffer)[position + 2] = (TUINT8) ((data&0x00FF0000) >> 16);
    ((TUINT8*) buffer)[position + 3] = (TUINT8) ((data&0xFF000000) >> 24);
}

#ifndef USE_STANDARD_LIBRARIES
void Memory_Fill(void*   memory,
                 TUINT8  value,
                 TUINT32 size)
{
    TUINT8* ptr;
    TUINT32 i;

    if (memory)
    {
        for (ptr = (TUINT8*) memory, i = 0; i < size; i++)
        {
            *ptr++ = value;
        }
    }
}

void Memory_Copy(void*       dest,
                 const void* src,
                 TUINT32     size)
{
    const TUINT8* sptr;
    TUINT8*       dptr;
    TUINT32       i;

    if ((src) && (dest))
    {
        for (sptr = (const TUINT8*) src, dptr = (TUINT8*) dest, i = 0; i < size; i++)
        {
            *dptr++ = *sptr++;
        }
    }
}

TINT32 Memory_Compare(const void* memory1,
                      const void* memory2,
                      TUINT32 size)
{
    const TUINT8* ptr1;
    const TUINT8* ptr2;
    TUINT32       i;

    if (! memory1)
    {
        return (memory2 != 0 ? -1 : 0);
    }
    if (! memory2)
    {
        return 1;
    }
    for (ptr1 = (const TUINT8*) memory1, ptr2 = (const TUINT8*) memory2, i = 0; i < size; i++, ptr1++, ptr2++)
    {
        if (*ptr1 > *ptr2)
        {
            return 1;
        }
        if (*ptr1 < *ptr2)
        {
            return -1;
        }
    }
    return 0;
}

TUINT32 String_Length(const TUINT8* str)
{
    TUINT32 length;

    if (! str)
    {
        return 0;
    }
    for (length = 0; *str++ != '\0'; length++);
    return length;
}

void String_Copy(TUINT8*       dest,
                 const TUINT8* src)
{
    if ((dest != 0) && (src != 0))
    {
        do
        {
            *dest++ = *src;
        } while (*src++ != '\0');
    }
}

void String_Append(TUINT8*       dest,
                   const TUINT8* src)
{
    String_Copy(dest + String_Length(dest), src);
}

TINT32 String_Compare(const TUINT8* str1,
                      const TUINT8* str2)
{
    if (! str1)
    {
        return (str2 != 0 ? -1 : 0);
    }
    if (! str2)
    {
        return 1;
    }
    for (; (*str1 != '\0') || (*str2 != '\0'); str1++, str2++)
    {
        if (*str1 > *str2)
        {
            return 1;
        }
        if (*str1 < *str2)
        {
            return -1;
        }
    }
    return 0;
}
#endif /*USE_STANDARD_LIBRARIES*/

TUINT32 Read_DecNumber(const TUINT8* str)
{
    TUINT32 value;

    if (! str)
    {
        return 0;
    }
    value = 0;
    while ((*str >= '0') && (*str <= '9'))
    {
        value = value*10 + (*str - '0');
        str++;
    }
    return value;
}

TUINT32 Read_HexNumber(const TUINT8* str)
{
    TUINT32 value;

    if (! str)
    {
        return 0;
    }
    value = 0;
    while (1)
    {
        if ((*str >= '0') && (*str <= '9'))
        {
            value = value*16 + (*str - '0');
        }
        else if ((*str >= 'A') && (*str <= 'F'))
        {
            value = value*16 + (*str - 'A') + 10;
        }
        else if ((*str >= 'a') && (*str <= 'f'))
        {
            value = value*16 + (*str - 'a') + 10;
        }
        else
        {
            break;
        }
        str++;
    }
    return value;
}

/*****************************************************************************/
/* End of File */
